
import javax.swing.ImageIcon;
import javax.swing.event.EventListenerList;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JPanel;

public class MainMenu extends ScreenManager{
	
	private static final long serialVersionUID = 1L;
	private String userName = "";
	private EventListenerList listenerList = new EventListenerList();
	
	public MainMenu(String name){

		userName = name;
		JPanel mainMenu = new JPanel();
		

		
		ImageIcon creditsBg = new ImageIcon(getClass().getResource("bgs/creditsBg.jpg"));
		ImageIcon scoresBg = new ImageIcon(getClass().getResource("bgs/scoresBg.jpg"));
		ImageIcon setBg = new ImageIcon(getClass().getResource("bgs/setBg.jpg"));
		ImageIcon helpBg = new ImageIcon(getClass().getResource("bgs/helpBg.jpg"));
		ImageIcon musicBg = new ImageIcon(getClass().getResource("bgs/musicBg.jpg"));
		ImageIcon exitBg = new ImageIcon(getClass().getResource("bgs/exitBg.jpg"));
		ImageIcon playBg = new ImageIcon(getClass().getResource("bgs/playBg.jpg"));
		
		
		
		
		getContentPane().add(mainMenu);
		
		JButton creditsButton = new JButton("Credits", creditsBg);
		creditsButton.addActionListener( new ActionListener()
		{
		    public void actionPerformed(ActionEvent e)
		    {
		    	closeTab();
		    	CreditMenu creditMenu = new CreditMenu(userName);
		    	creditMenu.setVisible(true);
		    }
		});
		JButton gameMenuButton = new JButton("Play Game", playBg);
		gameMenuButton.addActionListener( new ActionListener()
		{
		    public void actionPerformed(ActionEvent e)
		    {
		    	closeTab();
		    	GameMenu gameMenu = new GameMenu(userName);
		    	gameMenu.setVisible(true);
		    }
		});
		JButton  leaderBoardMenuButton = new JButton("High Scores", scoresBg);
		leaderBoardMenuButton.addActionListener( new ActionListener()
		{
		    public void actionPerformed(ActionEvent e)
		    {
		    	closeTab();
		    	LeaderBoardMenu leaderboardMenu = new LeaderBoardMenu(userName);
		    	leaderboardMenu.setVisible(true);
		    }
		});
		JButton settingsMenuButton = new JButton("Settings", setBg);
		settingsMenuButton.addActionListener( new ActionListener()
		{
		    public void actionPerformed(ActionEvent e)
		    {
		    	closeTab();
		    	SettingsMenu settingsMenu = new SettingsMenu(userName);
		    	settingsMenu.setVisible(true);

		    }
		});
		JButton helpMenuButton = new JButton("Help", helpBg);
		helpMenuButton.addActionListener( new ActionListener()
		{
		    public void actionPerformed(ActionEvent e)
		    {
		    	closeTab();
		    	HelpMenu helpMenu = new HelpMenu(userName);
		    	helpMenu.setVisible(true);
		    }
		});
		JButton exitButton = new JButton("Exit", exitBg);
		exitButton.addActionListener( new ActionListener()
		{
		    public void actionPerformed(ActionEvent e)
		    {
		    	System.exit(0);
		    }
		});
		
		JButton musicButton = new JButton("Open Library", musicBg);
		musicButton.addActionListener(new ActionListener() {

		    public void actionPerformed(ActionEvent arg0) {
		    	firePlayerEvent(new SoundManager( this, "playSelectedSong"));
		    }
		});
		
		mainMenu.setLayout(null);
		creditsButton.setBounds(235, 305, 95, 55);
		gameMenuButton.setBounds(145, 65, 95, 55);
		leaderBoardMenuButton.setBounds(55, 225, 95, 55);
		settingsMenuButton.setBounds(55, 145, 95, 55);
		helpMenuButton.setBounds(235, 145, 95, 55);
		exitButton.setBounds(55, 305, 95, 55);
		musicButton.setBounds(235, 225, 95, 55);
		
		
		
		mainMenu.add(creditsButton);
		mainMenu.add(gameMenuButton);
		mainMenu.add(leaderBoardMenuButton);
		mainMenu.add(settingsMenuButton);
		mainMenu.add(helpMenuButton);
		mainMenu.add(exitButton);
		mainMenu.add(musicButton);
	}
	
	public String getName(){
		return userName;
	}
	private void closeTab(){
    	this.dispatchEvent(new WindowEvent(this,WindowEvent.WINDOW_CLOSING));
	}
	public void firePlayerEvent(SoundManager event)
	{
		Object[] listeners = listenerList.getListenerList();
		
		for ( int i = 0 ; i < listeners.length; i += 2 )
		{
			if (listeners[i] == SoundListener.class)
			{
				((SoundListener)listeners[i + 1]).playerEventOccurred(event);
			}
		}	
	}
}

